package com.atguigu.juc.atomics;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.LongBinaryOperator;

/**
 * @author yanglin
 * @create 2024-01-26 17:17 星期五
 * description:
 */
public class LongAdderAPIDemo {


    public static void main(String[] args) {
        
//        LongAdder longAdder = new LongAdder();
//        longAdder.increment();
//        longAdder.increment();
//        longAdder.increment();
//        System.out.println(longAdder.sum());

//        LongAccumulator longAccumulator = new LongAccumulator((x,y) -> x + y,0);
//        longAccumulator.accumulate(1);
//        longAccumulator.accumulate(3);
//        System.out.println(longAccumulator.get());

        LongAccumulator longAccumulator = new LongAccumulator(new LongBinaryOperator() {
            @Override
            public long applyAsLong(long left, long right) {
                //自定义计算方式
                return left + right;
            }
        }, 0);
        
        longAccumulator.accumulate(1);
        System.out.println(longAccumulator.get());
        longAccumulator.accumulate(3);
        System.out.println(longAccumulator.get());
    }
    
    
}
